// pages/teacher/teacherjoin/teacherjoin.js
import sendRequest from '../../rpromise/request'

Page({

    /**
     * 页面的初始数据
     */
    data: {
        student: [],
        ano: '',
        check:true,
    },
    enterJoin(e){
        // console.log(e.currentTarget)
        var index = e.currentTarget.dataset.id
        var sno = this.data.student[index].sno
        var incheck = this.data.student[index].incheck
        var outcheck = this.data.student[index].outcheck
        var ano = this.data.ano
        var that = this
        var sql = "UPDATE sa SET incheck='Y',outcheck='Y' WHERE sno='"+sno+"' and ano="+ano
        if(incheck==='N'&&outcheck==='N'){
            wx.showModal({
                title: '提示',
                content: '确认该学生已完成了吗？',
                complete: (res) => {
                  if (res.cancel) {
                    
                  }
                  if (res.confirm) {
                      this.sqlRequest(sql).then(res=>{
                          console.log(res.statusCode)
                          if(res.statusCode===200){
                              wx.showToast({
                                title: '已确认',
                                icon:'success'
                              })
                              that.setData({
                                  check:false
                              })
                          }else{
                              wx.showToast({
                                  title: '出错了，请重试',
                                  icon:'error'
                                })
                          }
                      })
                  }
                }
              })
        }
        
        
    },
    loadData() {
        var sql = "SELECT student.sno,sname,sd,incheck,outcheck FROM student,sa WHERE sa.sno=student.sno and ano="+this.data.ano+" and student.sno in(SELECT sno FROM sa WHERE ano="+this.data.ano+")"
        this.sqlRequest(sql).then(res=>{
            console.log(res.data)
            this.setData({
                student:res.data
            })
        })
    },
    async sqlRequest(sql) {

        const res1 = await sendRequest({
            method: 'POST',
            url: '/manage',
            data: {
                sql: sql,
            },
        })
        return res1
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log(options)
        this.setData({
            ano: options.ano
        })
        this.loadData()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})